package com.twu.biblioteca.data;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: zgq
 * Date: 13-7-4
 * Time: 下午10:34
 * To change this template use File | Settings | File Templates.
 */
public class MovieBase {
    private String bibliotecaMovieDataBaseName;

    public MovieBase(String bibliotecaMovieDataBaseName) {
        this.bibliotecaMovieDataBaseName = bibliotecaMovieDataBaseName;
    }

    public List<String> movieList() {
        List<String> movieList = new ArrayList<String>();
        try {
            BufferedReader reader = new BufferedReader(new FileReader(new File(bibliotecaMovieDataBaseName)));
            String line="";
            while((line=reader.readLine())!=null) {
                if(constructMovie(line)!=null)
                    movieList.add(constructMovie(line).toString());
            }
            reader.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            return movieList;
        }
    }

    private Movie constructMovie(String line) {
        String [] movieItem = line.split("\t");
        if(movieItem.length==4) {
            Movie movie = new Movie(movieItem[0], Integer.parseInt(movieItem[1]), movieItem[2]);
            movie.grade(Integer.parseInt(movieItem[3]));
            return  movie;
        }
        return null;
    }
}